package cn.simple.model.base;

import cn.simple.strategy.DamageStrategy;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 格斗游戏角色基类
 */
public abstract class FightingGameRole extends Role {

    @Override
    protected void useSkill(Skill skill, List<Role> roleList) {
        //先执行技能策略，查看是对单体，还是对群，如果不存在技能策略，默认单体
        List<Role> targetRoles = Optional.ofNullable(skill.getSkillStrategy())
                .map(skillStrategy -> skillStrategy.execute(roleList)).orElse(Collections.singletonList(roleList.get(0)));
        //执行技能
        DamageStrategy damageStrategy = skill.getDamageStrategy();
        //伤害策略不存在则全体统一基础伤害
        if (damageStrategy == null) {
            useSkillBaseDamage(skill, targetRoles);
        } else {
            damageStrategy.execute(targetRoles);
        }
        //后置处理，将对应hp<=0的角色设置为死亡
        targetRoles.forEach(Role::checkAliveAndThenSet);
    }

    protected void useSkillBaseDamage(Skill skill, List<Role> roleList) {
        roleList.forEach(role -> {
            role.setHp(role.getHp() - skill.getBaseHpDamage());
            role.setMp(role.getMp() - skill.getBaseMpDamage());
        });
    }

    protected void attack(List<Role> roles){
        roles.forEach(role -> {
            role.setHp(role.getHp() - getPower());
            role.checkAliveAndThenSet();
        });
    }

}
